//Version 0.1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Net;
using System.Net.Sockets;

namespace FiveChess
{
    public partial class MainWnd : Form
    {
        private Game            game=null;
        private IGameInterface  pIGame=null;
        private IDrawScreen     pIDraw=null;
        private Image           backgroundImage=null;
        private IPlayer AI = null;

        private void GameStatusChanged(object sender, StatusChangedEventArgs e)
        {
            picChessBoard.Invalidate();
        }

        public MainWnd()
        {
            InitializeComponent();
            //Load BackgroundImage
            backgroundImage = Bitmap.FromFile("images/background.bmp"); 
            picChessBoard.BackgroundImage = backgroundImage;

            //Initialize game component 
            game= new Game();
            game.StatusChanged  += new GameStatusHandle(GameStatusChanged);
            pIGame = game;
            pIDraw = game;


            //TEST
            //AI = new AIPlayer();
            AI = new OnlinePlayer();
            game.AIPlayer = AI;
            game.Type  = GameType.Online ;
            //END
        }
      
        private void picChessBoard_MouseClick(object sender, MouseEventArgs e)
        {
            ChessmanType chessType = ChessmanType.Black;
            Point chessPoint = CaculatePoint(e.X,e.Y);

            if (checkBoxBlack.CheckState == CheckState.Checked)
            {
                chessType = ChessmanType.Black;
                checkBoxWhite.Checked = true;
                checkBoxBlack.Checked = false;
            }
            else
            {
                chessType = ChessmanType.White;
                checkBoxWhite.Checked = false;
                checkBoxBlack.Checked = true;
            }

            if (!pIGame.AddChessman(chessType, chessPoint))
            {
                MessageBox.Show("Invalid Position");
            }
            picChessBoard.Invalidate();
            return;

        }

        private void picChessBoard_Paint(object sender, PaintEventArgs e)
        {
            pIDraw.Draw(e.Graphics);
        }

        private void checkBoxBlack_CheckedChanged(object sender, EventArgs e)
        {

            if (checkBoxBlack.CheckState == CheckState.Checked)
            {
                 checkBoxWhite.Checked = false;
            }
        }

        private void checkBoxWhite_CheckedChanged(object sender, EventArgs e)
        {


            if (checkBoxWhite.CheckState == CheckState.Checked)
            {
                checkBoxBlack.Checked = false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="left"></param>
        /// <returns></returns>
        private int CaculateLeftPos(int left)
        {
            int lRetrun;
            int tmp;
            if (left > PublicConfigParameter.HalfSpanLen)
            {
                left = left - PublicConfigParameter.HalfSpanLen;
                left = left - left % PublicConfigParameter.SpanLen;
            }
            else
            {
                left = 0;
            }
            tmp = left + PublicConfigParameter.HalfSpanLen;
            tmp = tmp / PublicConfigParameter.SpanLen;
            lRetrun = tmp;
            return lRetrun;
        }

        private int CaculateTopPos(int top)
        {
            int lRetrun;
            int tmp;
            if (top > PublicConfigParameter.HalfSpanLen)
            {
                top = top - PublicConfigParameter.HalfSpanLen;
                top = top - top % PublicConfigParameter.SpanLen;
            }
            else
            {
                top = 0;
            }
            tmp = top + PublicConfigParameter.HalfSpanLen;
            tmp = tmp / PublicConfigParameter.SpanLen;
            lRetrun = tmp;
            return lRetrun;
        }

        private Point CaculatePoint(int left, int top)
        {
            Point rltPoint = new Point();
            rltPoint.X = CaculateLeftPos(left);
            rltPoint.Y = CaculateTopPos(top);
            return rltPoint;

        }

        [Obsolete("Will be removed in release version")]
        private void btnTest_Click(object sender, EventArgs e)
        {
            //pIGame.Undo();
            //picChessBoard.Invalidate();

        }

        private void MainWnd_Load(object sender, EventArgs e)
        {

        }

    }



}